#include "eqctrlmodel.h"
#include "dm/dmdef.h"
#include "dm/filter.h"

#include <QBrush>


EqCtrlModel::EqCtrlModel(QObject *parent)
    : QAbstractTableModel{parent}
    , m_row{-1}
{
    m_header = QStringList{ tr(LABEL_FREQ), tr(LABEL_Q_VALUE), tr(LABEL_GAIN), tr(LABEL_TYPE) };
}

int EqCtrlModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_list.size();
}

int EqCtrlModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_header.size();
}

QVariant EqCtrlModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) return QVariant();
    if (index.row() < m_list.size()) {
        if (role == Qt::DisplayRole) {
            auto cell = m_list.at(index.row());
            switch (index.column()) {
            case 0:
                return cell.freq;
            case 1:
                return Filter::bwToQ(cell.bw);
            case 2:
                return QString::number(cell.gain, 'f', EQ_GAIN_DECIMAL);
            case 3:
                return EQ_TYPE.at(static_cast<size_t>(cell.type));
            default:
                return QVariant();
            }
        }
        if (m_row == index.row() && role == Qt::BackgroundRole) {
            return QBrush(QColor(173, 216, 230));
        }
    }

    return QVariant();
}

bool EqCtrlModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole) {
        auto &cell = m_list[index.row()];
        switch (index.column()) {
        case 0:
            cell.freq = value.toUInt();
            break;
        case 1:
            cell.bw = Filter::qToBw(value.toDouble());
            break;
        case 2:
            cell.gain = value.toDouble();
            break;
        case 3:
            cell.type = static_cast<EQType>(value.toUInt());
            break;
        default:
            return false;
        }
        // m_row = index.row();
        emit dataModified(index.row(), cell);

        return true;
    }
    return false;
}

Qt::ItemFlags EqCtrlModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;

    return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
}

QVariant EqCtrlModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Horizontal) {
        if(Qt::DisplayRole == role && section < m_header.count()) {
            return m_header[section];
        }
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}

void EqCtrlModel::setList(QList<EQ> &list)
{
    beginResetModel();
    m_list = list;
    endResetModel();
}

void EqCtrlModel::setEq(const int &index, const EQ &eq)
{
    if (index > -1 && index < m_list.size()) {
        beginResetModel();
        m_list[index] = eq;
        endResetModel();
        emit dataModified(index, eq);
        m_row = index;
    }

}

void EqCtrlModel::activateEdit(const QModelIndex &index)
{
    if (index.isValid()) {
        m_row = index.row();
    }
}


